package me.evis.gae.guestbook.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class GuestBook implements EntryPoint {

    /**
     * Create a remote service proxy to talk to the server-side Comment service.
     */
    private final CommentServiceAsync commentService = 
        GWT.create(CommentService.class);

    @Override
    public void onModuleLoad() {
        final TextBox name = 
            TextBox.wrap(RootPanel.get("nameTxt").getElement());
        final TextArea comment = 
            TextArea.wrap(RootPanel.get("commentTxt").getElement());
        final Button addComment = 
            Button.wrap(RootPanel.get("addCommentBtn").getElement());
        final HTML comments = 
            HTML.wrap(RootPanel.get("commentsDiv").getElement());

        showComments(comments);
        
        // Create the popup dialog box
        final DialogBox dialogBox = new DialogBox();
        dialogBox.setText("Add Comment Done");
        dialogBox.setAnimationEnabled(true);
        final Button closeButton = new Button("Close");
        // We can set the id of a widget by accessing its Element
        closeButton.getElement().setId("closeButton");
        final Label serverResponseLabel = new Label();
        VerticalPanel dialogVPanel = new VerticalPanel();
        dialogVPanel.add(serverResponseLabel);
        dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
        dialogVPanel.add(closeButton);
        dialogBox.setWidget(dialogVPanel);

        closeButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                dialogBox.hide();
                addComment.setEnabled(true);
                showComments(comments);
            }
        });

        addComment.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                addComment.setEnabled(false);
                String username = name.getText();
                String commentText = comment.getText();
                commentService.addComment(username, 
                                          commentText,
                                          new AsyncCallback<String>() {
                        @Override
                        public void onFailure(Throwable caught) {
                            serverResponseLabel.setText("Error.");
                            dialogBox.center();
                        }

                        @Override
                        public void onSuccess(String result) {
                            serverResponseLabel.setText(result);
                            dialogBox.center();
                        }
                    });
            }
        });
    }
    
    private void showComments(final HTML comments) {
        commentService.listComment(new AsyncCallback<String>() {
                @Override
                public void onFailure(Throwable caught) {
                    comments.setHTML("Error.");
                }
                
                @Override
                public void onSuccess(String result) {
                    comments.setHTML(result);
                }
            });
    }

}
